package com.app.micaihu.view.main.dialog

import android.content.DialogInterface
import android.content.Intent
import com.app.base.BaseDialogFragment
import com.app.micaihu.R
import com.app.micaihu.configure.Config
import com.app.micaihu.configure.ConfigKeyConstant
import com.app.micaihu.view.user.userinfo.activity.WebActivity
import com.app.micaihu.view.utils.DownloadAgainUtils
import com.app.micaihu.view.utils.ViewUtils
import com.app.micaihu.view.utils.loadImage
import com.app.utils.util.DateUtils
import com.blankj.utilcode.util.SPUtils
import kotlinx.android.synthetic.main.mian_popup_dialog.*

class MainPopupDialogFragment : BaseDialogFragment() {

    override fun getLayoutId() = R.layout.mian_popup_dialog

    override fun setData() {
        val popupImg = SPUtils.getInstance().getString(ConfigKeyConstant.popupImg, "")
        ViewUtils.changeViewScale(ivImage, 0, 57, 66, 0)
        ivImage.loadImage(popupImg)
    }

    override fun addListeners() {
        ivClose.setOnClickListener { dismiss() }
        ivImage.setOnClickListener {
            dismiss()
            SPUtils.getInstance().put(ConfigKeyConstant.isShowPopupDialogData, DateUtils.getCurrentTime(DateUtils.formatDefault))
            val popupUrl = SPUtils.getInstance().getString(ConfigKeyConstant.popupUrl, "")
            val intent = Intent(context, WebActivity::class.java)
            intent.putExtra(Config.Extra.url, popupUrl)
            startActivity(intent)
        }
    }

    override fun onDismiss(dialog: DialogInterface) {
        super.onDismiss(dialog)
        SPUtils.getInstance().put(ConfigKeyConstant.isShowPopupDialogData, DateUtils.getCurrentTime(DateUtils.formatDefault))
        DownloadAgainUtils.showDownLoadDialog(parentFragmentManager)
    }

    override fun canceledOnTouchOutside() = true
}